package cn.gson.boot.controller;

import cn.gson.boot.model.pojos.*;
import cn.gson.boot.model.service.AttributeService;
import cn.gson.boot.model.service.PurchaseReturnService;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/purchaseReturn")
public class PurchaseReturnController {
    @Autowired
    PurchaseReturnService purchaseReturnService;

    /**
     * 采购退货分页查询
     * @param search
     * @param type
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping("/purchaseReturnList")
    @ResponseBody
    public Map<String,Object> purchaseReturnList(String search,String type, Integer pageNum, Integer pageSize, HttpSession session){
        Page<Object> obj = PageHelper.startPage(pageNum,pageSize);
        User us = (User)session.getAttribute("user");
        List<UserLogin> users = (List<UserLogin>)session.getAttribute("users");
        List<Role> roles = users.get(0).getRoleListss();
        List<String> jueNameList = new ArrayList<>();
        for(int i=0;i<roles.size();i++){
            jueNameList.add(roles.get(i).getJueName());
        }
        String empName = null;
        if(jueNameList.indexOf("boss")>-1 ||jueNameList.indexOf("销售经理")>-1  ){
            empName = null;
        }else{
            empName = us.getEmpName();
        }
        purchaseReturnService.selectPurchaseReturn(search,type);
        Map<String,Object> map = new HashMap<>();
        map.put("total",obj.getTotal());
        map.put("rows",obj.getResult());
        return map;
    }

    /**
     * 查询单号
     * @return
     */
    @RequestMapping("/purchaseReturnOrder")
    @ResponseBody
    public String purchaseReturnOrder(){
        return purchaseReturnService.PurchaseOrder();
    }

    /**
     * 查询采购单
     * @return
     */
    @RequestMapping("/purchaseList")
    @ResponseBody
    public List<Purchase> purchaseList(){
        return purchaseReturnService.purchaseList();
    }

    /**
     * 查询采购详情
     * @param purchaseId
     * @return
     */
    @RequestMapping("/selectPurchse")
    @ResponseBody
    public List<Purchase> selectPurchse(Integer purchaseId){
        return purchaseReturnService.selectPurchse(purchaseId);
    }

    /**
     * 查询采购退货单状态
     * @param purReturnId
     * @return
     */
    @RequestMapping("/purchaseReturnStatus")
    @ResponseBody
    public String purchaseReturnStatus(Integer purReturnId){
        return purchaseReturnService.purchaseReturnStatus(purReturnId);
    }

    /**
     * 新增时查询采购单状态
     * @param purchaseId
     */
    @RequestMapping("/selectPurchaseReturnStatus")
    @ResponseBody
    public List<PurchaseDetails> selectPurchaseReturnStatus(Integer purchaseId){
        return purchaseReturnService.selectPurchaseReturnStatus(purchaseId);
    }

    /**
     * 新增采购退货单和采购退货详情
     * @param PurchaseReturn
     */
    @RequestMapping("/addPurchaseReturn")
    @ResponseBody
    public void addPurchaseReturn(String PurchaseReturn){
        JSONObject obj = (JSONObject)JSONObject.parse(PurchaseReturn);
        List purList = obj.getObject("purList",List.class);
        PurchaseReturn purchaseReturn = obj.getObject("PurchaseReturn", PurchaseReturn.class);
        purchaseReturnService.addPurchaseReturn(purchaseReturn,purList);
    }

    /**
     * 查询需要修改的采购退货单
     * @param purReturnId
     */
    @RequestMapping("/selectPurchaseReturnDetails")
    @ResponseBody
    public List<ReturnDetails> selectPurchaseReturnDetails(Integer purReturnId){
        return purchaseReturnService.selectPurchaseReturnDetails(purReturnId);
    }

    /**
     * 删除采购退货单
     * @param purReturnId
     */
    @RequestMapping("/delpurchaseReturn")
    @ResponseBody
    public void delpurchaseReturn(Integer purReturnId){
        purchaseReturnService.delpurchaseReturn(purReturnId);
    }


    //根据id查到对象
    @RequestMapping("/selectPurchaseReturnByPurreturnId")
    @ResponseBody
    public PurchaseReturn selectPurchaseReturnByPurreturnId(Integer purreturnId){
        return purchaseReturnService.selectPurchaseReturnByPurreturnId(purreturnId);
    }
}
